package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.apis.article.ArticleClient;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmNewsPageDto;
import com.heima.utils.common.DateUtils;
import com.heima.wemedia.service.WmStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WmStatisticsServiceImpl implements WmStatisticsService {
    @Autowired
    private ArticleClient articleClient;

    /**
     * 分页查询 图文统计
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult newsPage(WmNewsPageDto dto) {
        PageResponseResult responseResult = articleClient.newPage(dto);
        return responseResult;
    }

    /**
     * 图文统计
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult newsDimension(String beginDate, String endDate) {
        Date beginDateTime = DateUtils.stringToDate(beginDate);
        Date endDateTime = DateUtils.stringToDate(endDate);
        Map<String, Object> resultMap = new HashMap<>();

        ResponseResult responseResult = articleClient.newsDimension(beginDateTime, endDateTime);
        if (responseResult.getCode().equals(200)) {
            String jsonString = JSON.toJSONString(responseResult.getData());
            Map map = JSON.parseObject(jsonString, Map.class);
            resultMap.put("likesNum", map.get("likesNum") == null ? 0 : map.get("likesNum"));
            resultMap.put("collectNum", map.get("collectNum") == null ? 0 : map.get("collectNum"));
            resultMap.put("publishNum", map.get("publishNum") == null ? 0 : map.get("publishNum"));
        }
        return ResponseResult.okResult(resultMap);
    }
}
